'use strict';

const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
const moment = require('moment');

class HomeController extends Controller {
  async index() {
    // this.ctx.body = 'hi, egg';
    await this.ctx.render(
      'index.html',
      {

      }, {
        layout: 'layout.html',
      });
  }

  async login() {
    await this.ctx.render(
      'page-login.html',
      {

      },
      {
        layout: 'layout.html',
      });
  }

  async loginAction() {
    const { username, password } = this.ctx.request.query;
    if (!username || !password) {
      this.ctx.body = { status: -1, message: '参数不完整' };
      return;
    }
    const originalUser = await this.ctx.model.User.findOne({ username });
    if (!originalUser) {
      this.ctx.body = { status: -1, message: '用户不存在' };
      return;
    }
    if (password === originalUser.password) {
      this.ctx.cookies.set('token', originalUser._id, {
        httpOnly: false,
      });
      this.ctx.body = { status: 0, message: 'success' };
    } else {
      this.ctx.body = { status: -1, message: '密码错误' };
    }
  }

  async banner() {
    const bannerData = await this.ctx.model.Banner.find({}).sort({ weight: -1 });
    await this.ctx.render('banner.html', { bannerData, csrf: this.ctx.csrf }, { layout: 'layout.html' });
  }
  async delBanner() {
    const { id } = this.ctx.request.body;
    await this.ctx.model.Banner.remove({ _id: id });
    this.ctx.body = { status: 0, message: 'success' };
  }
  async bannerAdd() {
    const { file, link, weight } = this.ctx.request.body;
    if (file && link && weight) {
      await (new this.ctx.model.Banner({ url: file, link, weight: +weight || 0 })).save();
    }
    this.ctx.redirect('/admin/banner');
  }

  async consult() {
    const { page } = this.ctx.request.query;
    const data = await this.ctx.model.Article.find({}, { content: 0 })
    // .limit(20).skip(((+page || 1) - 1) * 20)
      .sort({ createTime: -1 });
    data.forEach((v, k) => {
      data[k] = v.toObject();
      data[k].createTime = moment(data[k].createTime).format('YYYY-MM-DD HH:mm');
    });
    await this.ctx.render('consult.html', { data }, { layout: 'layout.html' });
  }
  async consultDetail() {
    const { id } = this.ctx.params;
    const data = await this.ctx.model.Article.findOne({ _id: id });
    await this.ctx.render('updateConsult.html', { data, id }, { layout: 'layout.html' });
  }
  async delArticle() {
    const { id } = this.ctx.request.body;
    await this.ctx.model.Article.remove({ _id: id });
    this.ctx.body = { status: 0, message: '删除成功' };
  }

  async project() {
    const { page } = this.ctx.request.query;
    const data = await this.ctx.model.Project.find({}, { content: 0 })
    // .limit(20).skip(((+page || 1) - 1) * 20)
      .sort({ createTime: -1 });
    data.forEach((v, k) => {
      data[k] = v.toObject();
      data[k].createTime = moment(data[k].createTime).format('YYYY-MM-DD HH:mm');
    });
    await this.ctx.render('project.html', { data }, { layout: 'layout.html' });
  }
  async projectDetail() {
    const { id } = this.ctx.params;
    const data = await this.ctx.model.Project.findOne({ _id: id });
    await this.ctx.render('updateProject.html', { data, id }, { layout: 'layout.html' });
  }
  async updateProject() {
    const { id, title, avatar, content } = this.ctx.request.body;
    await this.ctx.model.Project.update({ _id: id }, { title, avatar, content });
    this.ctx.body = { status: 0, message: 'success' };
  }
  async uploadArticle() {
    const { avatar, content, title } = this.ctx.request.body;
    try {
      await (new this.ctx.model.Article({ avatar, content, title, createTime: new Date() })).save();
      this.ctx.body = { status: 0, message: 'success' };
      return;
    } catch (e) {
      this.ctx.body = { status: -2, message: e };
      return;
    }
  }
  async updateArticle() {
    const { avatar, content, title, id } = this.ctx.request.body;
    try {
      await this.ctx.model.Article.update({ _id: id }, { avatar, content, title });
      this.ctx.body = { status: 0, message: 'success' };
      return;
    } catch (e) {
      this.ctx.body = { status: -2, message: e };
      return;
    }
  }
  async uploadProject() {
    const { avatar, content, title } = this.ctx.request.body;
    try {
      let seq = 0;
      const arrPro = await this.ctx.model.Project.findOne({}).sort({ createTime: -1 });
      if (arrPro) {
        seq = arrPro.seq + 1;
      }
      await (new this.ctx.model.Project({ avatar, content, title, createTime: new Date(), seq })).save();
      this.ctx.body = { status: 0, message: 'success' };
      return;
    } catch (e) {
      this.ctx.body = { status: -2, message: e };
      return;
    }
  }
  async updateHistory() {
    let { id, history } = this.ctx.request.body;
    history = history === 'true';
    await this.ctx.model.Article.update({ _id: id }, { history });
    this.ctx.body = { status: 0, message: 'success' };
  }

  async notification() {
    let data = await this.ctx.model.Notification.findOne();
    data = data.toObject();
    data.startTime = moment(data.startTime).format('YYYY-MM-DD');
    data.endTime = moment(data.endTime).format('YYYY-MM-DD');
    await this.ctx.render('notification.html', { data }, { layout: 'layout.html' });
  }

  async uploadNotification() {
    let { content, title, avatar, startTime, endTime } = this.ctx.request.body;
    startTime = new Date(startTime);
    endTime = new Date(endTime);
    try {
      const data = await this.ctx.model.Notification.findOne({});
      if (data) {
        await this.ctx.model.Notification.update({ _id: data._id }, { content, title, avatar, startTime, endTime });
      } else {
        await (new this.ctx.model.Notification({ content, title, avatar, startTime, endTime })).save();
      }
      this.ctx.body = { status: 0, message: 'success' };
    } catch (e) {
      this.ctx.body = { status: -1, message: e };
    }
  }
}

module.exports = HomeController;
